﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using Billing.Controls;
using Billing.Utilities;
using System.Configuration;

namespace Billing
{
    public partial class NewBulkPrinter : Form
    {
        PaperSize psize;
        string BillNo;
        int cnt = 0;
        double BoxPrice = 0;
        int Qty = 0;

        public NewBulkPrinter(string billNo)
        {
            InitializeComponent();
            BillNo = billNo.ToUpper();
        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            double totalQty = 0;

            e.PageSettings.PaperSize = psize;
            e.Graphics.DrawString("KS BAKERS", new Font(new FontFamily("Times New Roman"), 12, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 100, 0);
            e.Graphics.DrawString(ConfigurationManager.AppSettings["ColonyName"], new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 15, 15);
            e.Graphics.DrawString(ConfigurationManager.AppSettings["TinNo"], new Font(new FontFamily("Times New Roman"), 11, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 80, 30);
            e.Graphics.DrawString("Date : " + DateTime.Now.ToString("dd/MM/yy") + "  " + DateTime.Now.ToShortTimeString() + "    Bill No:" + BillNo, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 9, 50);
            e.Graphics.DrawString("Cashier Name : " + Storage.LoginUser, new Font(new FontFamily("Times New Roman"), 9, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, 65);

            DataSet dtValue = Database.GetBulkOrderDetails(BillNo);

            BoxPrice = double.Parse(dtValue.Tables[0].Rows[0]["EachBoxCost"].ToString());
            Qty = int.Parse(dtValue.Tables[0].Rows[0]["Qty"].ToString());

            int lineCounter = 80;
            e.Graphics.DrawString("Customer Name : " + dtValue.Tables[0].Rows[0]["CustomerName"].ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 9, lineCounter);
            lineCounter += 15;
            e.Graphics.DrawString("Phone No : " + dtValue.Tables[0].Rows[0]["PhoneNo"].ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 9, lineCounter);
            lineCounter += 15;
            e.Graphics.DrawString("Delivery Date : " + DateTime.Parse(dtValue.Tables[0].Rows[0]["DeliveryDate"].ToString()).ToString("dd-MMM-yyyy") + "  " + dtValue.Tables[0].Rows[0]["DeliveryTime"].ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 9, lineCounter);
            lineCounter += 15;
            e.Graphics.DrawString("Description : " + dtValue.Tables[0].Rows[0]["Description"].ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 9, lineCounter);
            lineCounter += 15;
            e.Graphics.DrawString("Delivery Place : " + dtValue.Tables[0].Rows[0]["DeliveryPlace"].ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 9, lineCounter);

            //lineCounter += 15;
            //e.Graphics.DrawString("Advance :" + dtValue.Tables[0].Rows[0]["Advance"].ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 9, lineCounter);
            //lineCounter += 15;
            //e.Graphics.DrawString("Balance :" + dtValue.Tables[0].Rows[0]["Balance"].ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 9, lineCounter);

            string discount = string.Empty;
            if (dtValue.Tables[0].Rows[0]["DiscountByCost"].ToString() != "0")
            {
                lineCounter += 15;
                e.Graphics.DrawString("Discount Amount : " + dtValue.Tables[0].Rows[0]["DiscountByCost"].ToString() + "/-", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 9, lineCounter);
            }
            else if (dtValue.Tables[0].Rows[0]["DiscountByPercentage"].ToString() != "0")
            {
                lineCounter += 15;
                e.Graphics.DrawString("Discount Percentage : " + dtValue.Tables[0].Rows[0]["DiscountByPercentage"].ToString() + " %", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 9, lineCounter);
            }

            if (BoxPrice != 0)
            {
                lineCounter += 15;
                e.Graphics.DrawString("Each Box Cost : " + BoxPrice.ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 9, lineCounter);

                lineCounter += 15;
                e.Graphics.DrawString("Qty : " + Qty.ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 9, lineCounter);
            }

            //if (discount != string.Empty)
            //{
            //    lineCounter += 15;
            //    e.Graphics.DrawString("Discount :" + discount, new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 9, lineCounter);
            //}

            lineCounter += 15;
            e.Graphics.DrawString("--------------------------------------------------------------", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, lineCounter);
            lineCounter += 10;
            if (BoxPrice == 0)
                e.Graphics.DrawString("Item Name                    Rate        Qty       Amount", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, lineCounter);
            else
                e.Graphics.DrawString("Item Name                    Rate        Qty", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, lineCounter);
            lineCounter += 10;
            e.Graphics.DrawString("--------------------------------------------------------------", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, lineCounter);
            lineCounter += 15;

            foreach (DataRow row in dtValue.Tables[1].Rows)
            {
                totalQty += double.Parse(row[2].ToString());

                e.Graphics.DrawString(row[1].ToString(), new Font(new FontFamily("Times New Roman"), 9, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, lineCounter);
                e.Graphics.DrawString(row[3].ToString(), new Font(new FontFamily("Times New Roman"), 9, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 155, lineCounter);
                e.Graphics.DrawString(row[2].ToString(), new Font(new FontFamily("Times New Roman"), 9, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 205, lineCounter);
                if (BoxPrice == 0)
                    e.Graphics.DrawString(row[4].ToString(), new Font(new FontFamily("Times New Roman"), 9, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 260, lineCounter);

                lineCounter += 15;
            }
            cnt = dtValue.Tables[1].Rows.Count;

            e.Graphics.DrawString("--------------------------------------------------------------", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 10, lineCounter);
            lineCounter += 15;
            if (BoxPrice == 0)
                e.Graphics.DrawString("Items : " + dtValue.Tables[1].Rows.Count + "                          Total Qty : " + totalQty.ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 20, lineCounter);
            else
                e.Graphics.DrawString("Items : " + dtValue.Tables[1].Rows.Count + "/box                      Total Boxes : " + Qty.ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 20, lineCounter);

            lineCounter += 15;
            e.Graphics.DrawString("   Advance : " + dtValue.Tables[0].Rows[0]["Advance"].ToString() + " /-           Balance : " + dtValue.Tables[0].Rows[0]["Balance"].ToString() + " /-", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 9, lineCounter);
            //lineCounter += 15;
            //e.Graphics.DrawString("Balance :" + dtValue.Tables[0].Rows[0]["Balance"].ToString(), new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 9, lineCounter);
            lineCounter += 20;
            e.Graphics.DrawString("Thank You", new Font(new FontFamily("Times New Roman"), 10, System.Drawing.FontStyle.Regular), System.Drawing.Brushes.Black, 115, lineCounter);
        }

        private void printDocument1_QueryPageSettings(object sender, System.Drawing.Printing.QueryPageSettingsEventArgs e)
        {
            psize = new PaperSize("saurabh", 300, 395 + cnt * 15);
            e.PageSettings.PaperSize = psize;
        }
    }
}
